<%--
  Created by IntelliJ IDEA.
  User: 86181
  Date: 2022/11/7
  Time: 14:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>入库申请</title>
</head>
<body>
        <form id="addgatherdetails">
            <br>
            <div class="row">
            <div class="col-md-5">
                <div class="btn-group">
                    <button type="button" onclick="queryByqueryfiles()" class="btn btn-default">添加产品</button>
                    <button type="button" onclick="deleteather()" class="btn btn-default">删除产品</button>
                    <button type="reset" class="btn btn-default">清空</button>
                    <input  value="提交" type="submit" onclick="addapplyahter()" class="btn btn-default">
                </div>
                <br>
                <span style="color:deepskyblue">您正在做的业务是：库存管理--入库申请管理--入库申请登记</span>
            </div>
            <div  class="col-md-7"> <h2>入库申请</h2></div>
        </div>
            <div style="height:100%;width: 1200px;border: black 2px solid">

                <div align="center" class="container">
                    <br>
                    <div class="row">
                        <div class="col-md-6">
                            <table>
                                <tr>
                                    <td>入库人:</td>
                                    <td><input type="text" id="storerzhd" name="storer" class="form-control" required style="outline: none;border:none;border-bottom:black 1px dashed"></td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table>
                                <tr>
                                    <td>入库理由:</td>
                                    <td>
                                        <select class="form-control" id="reasonzhds">
                                            <option value="R001-1">生产入库</option>
                                            <option value="R001-2">库存初始</option>
                                            <option value="R001-3">赠送</option>
                                            <option value="R001-4">内部借领</option>
                                            <option value="R001-5">其他借领</option>
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <br>
                    <div>
                        <table  class="table" id="addgoodscp">
                            <thead>
                            <tr style="background-color:deepskyblue">
                                <th>点选</th>
                                <th>产品名称</th>
                                <th>产品编号</th>
                                <th>描述</th>
                                <th>数量</th>
                                <th>单位</th>
                                <th>成本单价(元)</th>
                                <th>小计(元)</th>
                            </tr>
                            </thead>
                            <tbody>

                            </tbody>
                        </table>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-md-6">
                            <table id="gatherdetailstable">
                                <tr>
                                    <td>总件数:</td>
                                    <td><input type="text" name="amountSum"   id="amountSumahter" class="form-control" readonly style="outline: none; border:none;"></td>
                                </tr>
                                <tr>
                                    <td>登记人:</td>
                                    <td><input type="text" name="register" id="registerzhd"  class="form-control" required style="outline: none; border:none; border-bottom:black 1px dashed"></td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table>
                                <tr>
                                    <td>总金额:</td>
                                    <td><input type="text" name="costPriceSum" id="costPriceSumzhd"  class="form-control" readonly style="outline: none; border:none;"></td>
                                </tr>
                                <tr>
                                    <td>登记时间:</td>
                                    <td><input type="text"  id="querygathertimes"  readonly style="outline: none; border:none;"></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <br>
                    <div>
                        <div class="form-group">
                            <label for="updatemessage-textgoods" class="control-label">备注:</label>
                            <textarea class="form-control" name="remark" id="updatemessage-textgoods" style="width:1100px;height:200px"></textarea>
                        </div>
                    </div>
                </div>
                <!--查询所有产品模态框-->
                <div class="modal" tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel" id="addfeilqueryAll">
                    <div class="modal-dialog modal-lg">
                        <div class="modal-content">
                            <div class="modal-header">
                                <span style="color:deepskyblue">您正在做的业务是：库存管理--入库申请管理--入库申请登记</span>
                                <button class="close" data-dismiss="modal">退出</button>
                            </div>
                            <div class="modal-body">
                                <div>
                                    <div id="toolbarfilestackahter" class="col-lg-6 col-md-offset-2">
                                        <div class="input-group">
                                            <input type="text" class="form-control" id="fileproductathername" placeholder="请输入要查询的产品">
                                            <span class="input-group-btn">
                                 <button class="btn btn-primary" onclick="queryByFileather()">查询</button>
                                 </span>
                                        </div>
                                    </div>
                                    <!--查询所有的产品-->
                                    <table id="addfilestock">

                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
        <script>
                //查询所有产品
                function queryByqueryfiles() {
                    $("#addfeilqueryAll").modal("show")

                }

                $(function () {
                    $("#addfilestock").bootstrapTable({
                        url:"queryByFileStockather.action",
                        method:'get',                      //请求方式（*）
                        contentType: "application/x-www-form-urlencoded", //如果用tomcat就要写这个
                        toolbar:"#toolbarfilestackahter ",//工具按件用那个容器
                        striped: true,//是否显示隔行变色
                        cache: false,//是否缓存
                        pagination: true,//是否显示分页
                        sortable: true,//是否显示排序
                        sortOrder: "aso",//排序方式
                        sidePagination: "server", //分页方式：client客户端分页，server服务端分页（*）
                        pageNumber: 1,//初始化加载第一页，默认第一页,并记录
                        pageSize: 5,//每页记录数
                        pageList: [5, 10, 15, 20],//可供选择的每页的行数（*）
                        search: false,//是否显示输入框
                        strictSearch: true,
                        showColumns: true,//是否显示所有的列（选择显示的列）
                        showRefresh: true,//是否显示刷新按钮
                        minimumCountColumns: 2,//最少允许的列数
                        clickToSelect: true,//是否启用点击选中行
                        uniqueId: "id",//每一行的唯一标识，一般为主键列
                        showToggle: true,//是否显示详细视图和列表视图的切换按钮
                        cardView: false,//是否显示详细视图
                        detailView: false,//是否显示父子表
                        //获取查询到的参数
                        queryParams: function (params) {
                            var menu={
                                pagesize:params.limit,
                                pageno:(params.offset/params.limit)+1,
                                sort:params.sort,
                                sortOrder:params.sort,
                                productname:$("#fileproductathername").val()
                            }
                            return menu
                        },
                        columns:[
                            {
                                field:"productid",
                                title:"产品号",
                            },
                            {
                                field:'productname',
                                title:'产品名称',
                            },{
                                field:"type",
                                title:"用途类型",
                                formatter:function (value,row,index) {
                                    if(row.type=="Y001-1"){
                                        return "商品"
                                    }
                                    if(row.type=="Y001-2"){
                                        return "物料"
                                    }
                                }
                            },{
                                field:"productclass",
                                title:"档次级别",
                                formatter:function (value,row,index) {
                                    if(row.productclass=="D001-1"){
                                        return "高档"
                                    }
                                    if(row.productclass=="D001-2"){
                                        return "中档"
                                    }
                                    if(row.productclass=="D001-3"){
                                        return "低档"
                                    }
                                }
                            },{
                                field:"firstkindname",
                                title:"|级分类",
                            },{
                                field:"secondkindname",
                                title:"||级分类",
                            },{
                                field:"thirdkindname",
                                title:"|||级分类",
                            },
                            {
                                field:"costprice",
                                title:"成本单价",
                            },
                            {
                                field:"",
                                title:"入库",
                                formatter:function (value,row,index) {
                                    return "<input type='button' value='入库' onclick='addfilegather("+row.id+","+row.productid+")' class='btn btn-success'>"
                                }
                            }
                        ]

                    })
                })
                //点击获取当前时间
                $.get("queryCellByDate.action",function (data) {
                    var  datetimestack=data.datetime;
                    $("#querygathertimes").val(datetimestack)
                },"json")
                //查询刷新
                function queryByFileather() {
                    $("#addfilestock").bootstrapTable("refresh")
                }

                //入库
                function addfilegather(id,productid) {
                    var f=true;
                    $("#addgoodscp tbody tr").each(function (index,item) {
                        if($(item).find("input").eq(2).val()==productid){
                            f=false;
                        }

                    })
                    if(f){
                        var  ids;
                        //获取方法根据id查询添加到入库申请页面里
                        $.post("queryByIdaddather.action/"+id,function (data) {
                            tds=data.id;
                            //创建tr
                            var tr=$("<tr></tr>")
                            //创建td
                            var td=$("<td><input  class='checkzhd' type='checkbox'></td>")
                            var td1=$("<td><input name='productName' type='text' value='"+data.productname+"' readonly style='outline: none;border:none; width:100px'></td>")
                            var td2=$("<td><input name='productId' type='text' readonly value='"+data.productid+"' style='outline: none;border:none; width:160px' ></td>")
                            var td3=$("<td><input name='productDescribe' type='text' required style='outline: none;border:none; width:110px'></td>")
                            var td4=$("<td><input name='amount' type='text'  required onchange='amountzhd(this)' value='1' style='outline: none;border:none;width:100px'></td>")
                            var td5=$("<td><input name='amountUnit' type='text' readonly value='件' style='outline: none;border:none; width:100px'></td>")
                            var td6=$("<td><input name='costPrice' type='text' readonly  value='"+data.costprice+"' style='outline: none;border:none; width:100px'></td>")
                            var td7=$("<td><input name='subtotal' type='text' value='"+data.costprice+"' readonly   style='outline: none;border:none'></td>")
                            //追加
                            tr.append(td)
                            tr.append(td1)
                            tr.append(td2)
                            tr.append(td3)
                            tr.append(td4)
                            tr.append(td5)
                            tr.append(td6)
                            tr.append(td7)
                            $("#addgoodscp tbody").append(tr)
                        },"json")
                    }else {
                        alert("产品已经存在")
                    }

                }
                //数量失去焦点赋值
                function amountzhd(id){
                    var amount=$(id).val();
                    var costprice=$(id).parent().next().next().find(":eq(0)").val();
                    $(id).parent().next().next().next().find(":eq(0)").val(Number(amount)*Number(costprice))
                    var num=0;
                    var amounts=$("input[name='amount']");
                    $(amounts).each(function (index,item) {
                        num+=Number(item.value)
                    })
                    var nums=0;
                    var costprices=$("input[name='subtotal']")
                    $(costprices).each(function (index,item) {
                        nums+=Number(item.value)
                    })
                    //赋值给总件数
                    $("#costPriceSumzhd").val(nums)
                    $("#amountSumahter").val(num)
                }

                //入库提交
                function addapplyahter() {
                    $("#addgatherdetails").validate({
                        submitHandler:function () {
                            if($("#addgoodscp tr").length<=1) {
                                alert("请选择入库的产品")
                                return
                            }
                            var gatherdetailss=$("#addgoodscp tbody tr");
                            //创建对象
                            var gatherdetails =new Array();
                            gatherdetailss.each(function (index,n) {
                                gatherdetails.push({
                                    productId: $(n).find("input").eq(2).val(),
                                    productName:$(n).find("input").eq(1).val(),
                                    productDescribe:$(n).find("input").eq(3).val(),
                                    amount:$(n).find("input").eq(4).val(),
                                    amountUnit:$(n).find("input").eq(5).val(),
                                    costPrice:$(n).find("input").eq(6).val(),
                                    subtotal:$(n).find("input").eq(7).val()
                                })

                            })
                            //转成对象
                            var gather={
                                storer:$("#storerzhd").val(),
                                reason:$("#reasonzhds").val(),
                                reasonexact:$("#reasonzhds option:selected").text(),
                                amountSum:$("#amountSumahter").val(),
                                costPriceSum:$("#costPriceSumzhd").val(),
                                register:$("#registerzhd").val(),
                                // registerTime:$("#querygathertimes").val(),
                                remark:$("#updatemessage-textgoods").val(),
                                gatherDetails:gatherdetails,
                            }
                            console.log(gather)
                            $.ajax({
                                url:"addGatherGatherDetails.action",
                                data:JSON.stringify(gather),
                                contentType:"application/json",
                                type:"post",
                                dataType:"json",
                                success:function (data) {
                                    alert(data.msg)
                                    //页面跳转
                                    $("#tab1").addTabs("入库申请登记审核", "", "processather.jsp", true);
                                    $("#tab1").closeTabs("入库申请登记")
                                    $("#addgoodscp").bootstrapTable("refresh")


                                }
                            })
                        }
                    })
                   /* //获取入库数据
                    var arr=$("#addgatherdetails").serializeArray();
                    console.log(arr)
                    var gather={};
                    $(arr).each(function (index,item) {
                        gather[item.name]=item.value;
                    })
                    //从表格中获取数据
                    $("#gatherdetailstable").bootstrapTable("")*/


                }
                //删除入库产品
                function deleteather() {
                    if( $(".checkzhd:checked").length==0){
                        alert("请选择以下产品在操作")
                        return
                    }
                   if(confirm("您确定要删除该产品?")){
                       $(".checkzhd:checked").each(function (index,item) {
                           $(item).parent().parent().remove();
                       })
                   }

                }
        </script>
</body>
</html>
